package server;

import com.esotericsoftware.kryonet.Connection;
import java.util.ArrayList;
import java.util.List;
import util.Client;

public class ClientManager {

    private final List<Client> clients;

    public ClientManager() {
        clients = new ArrayList<>();
    }

    public Client getClientFromAddress(String ip) {
        for (Client client : clients) {
            if (client.getConnection().getRemoteAddressTCP().toString().equalsIgnoreCase(ip)) {
                return client;
            }
        }

        return null;
    }

    public Client getClientFromConnection(Connection connection) {
        for (Client client : clients) {
            if (client.getConnection().equals(connection)) {
                return client;
            }
        }

        return null;
    }

    public void addClient(Client client) {
        clients.add(client);
    }

    public void removeClient(Client client) {
        clients.remove(client);
    }

    public void removeClientByConnection(Connection connection) {
        for (int i = 0; i < clients.size(); i++) {
            Client client = clients.get(i);
            
            if (client.getConnection().equals(connection)) {
                clients.remove(client);
            }
        }
    }

    public List<Client> getClients() {
        return clients;
    }
}
